package com.ybxx.base.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;


import com.ybxx.base.model.ResHydropower;
import com.ybxx.base.model.Residents;
import com.ybxx.base.model.ResidentsMember;
import com.ybxx.base.model.Room;
import com.ybxx.base.model.RoomInfo;
import com.ybxx.base.model.Type;
import com.ybxx.base.service.ResHydropowerService;
import com.ybxx.base.service.ResidentsMemberService;
import com.ybxx.base.service.ResidentsService;
import com.ybxx.base.service.TypeService;
import com.ybxx.config.model.HousingProperty;
import com.ybxx.config.service.HousingPropertyService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.DateUtil;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;
import com.ybxx.system.SystemConstants;
import com.ybxx.system.model.User;

public class ResidentsAction extends BaseAction {
	@Resource
	private ResidentsService residentsService;
	@Resource
	private ResidentsMemberService residentsMemberService;
	@Resource
	private ResHydropowerService resHydropowerService;
	@Resource
	private HousingPropertyService housingPropertyService;
	@Resource
	private TypeService typeService;
	private List<Residents> residentsex;
	private String cid;
	private String bid;
	private String uid;
	private String rid;
	private String rcode;
	private Room room;
	private String tids;
	private String rmins;
	private String rmaxs;
	private String prices;
	private Residents residents;
	private ResidentsMember member;
	private List<HousingProperty> housingPropertys;
	private List<Type> types;
	private List<ResHydropower> hydropowers;
	private String msg="succ";
	/*加载需要浏览的房间信息*/
	public String view(){
		Object[] obj=this.residentsService.load(rid,rcode);
		residents=(Residents)obj[0];
		room=(Room)obj[1];
		housingPropertys=housingPropertyService.getAll();
		types=typeService.getAll();
		if(!StringUtils.isEmpty(rcode)){
			member=residentsMemberService.loadMember(rcode);
			hydropowers=resHydropowerService.loadResHydropowers(rcode);
		}
		return "view";
	}
	/*删除住户信息*/
	public String remove(){
		try{
			residentsService.removeAll(rcode);
		}catch(Exception ex){
			msg="住户信息删除失败";
		}finally{
			return "succ";
		}
	}
	/*保存住户信息*/
	public String save(){
		try{
			if(StringUtils.isEmpty(residents.getRcode())){
				residents.setRcode(UUIDGenerator.getUUID());
			}
			List<ResHydropower> hydropowers=new ArrayList<ResHydropower>();
			if(!StringUtils.isEmpty(tids)){
				String[] tid=tids.split(",");
				String[] rmin=rmins.split(",");
				String[] rmax=rmaxs.split(",");
				String[] price=prices.split(",");
				for(int i=0;i<tid.length;i++){
					ResHydropower hydropower=new ResHydropower();
					hydropower.setRhid(UUIDGenerator.getUUID());
					hydropower.setResidents(residents);
					hydropower.setType(new Type(tid[i]));
					hydropower.setRmin(rmin[i]);
					hydropower.setRmax(rmax[i]);
					hydropower.setPrice(price[i]);
					hydropowers.add(hydropower);
				}
			}
			if(StringUtils.isEmpty(member.getMid())){
				member.setMid(UUIDGenerator.getUUID());
			}
			member.setResidents(residents);
			residentsService.save(residents,hydropowers,member);
		}catch(Exception ex){
			msg="住户维护失败";
		}finally{
			return "succ";
		}
	}
	/*加载需要修改的房间信息*/
	public String load(){
		Object[] obj=this.residentsService.load(rid,rcode);
		residents=(Residents)obj[0];
		room=(Room)obj[1];
		housingPropertys=housingPropertyService.getAll();
		types=typeService.getAll();
		if(!StringUtils.isEmpty(rcode)){
			member=residentsMemberService.loadMember(rcode);
			hydropowers=resHydropowerService.loadResHydropowers(rcode);
		}
		return "load";
	}
	public String queryPage(){
		QueryFilter filter=new QueryFilter(getRequest());
		/**获取当前登录者*/
		User user=(User)super.getSession().getAttribute(SystemConstants.SESSION_CURRENT_USER);
		residentsex=residentsService.queryPage(filter,cid,bid,uid,rid,user.getLoginid());
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "queryPage";
	}
	public List<Residents> getResidentsex() {
		return residentsex;
	}
	public void setResidentsex(List<Residents> residentsex) {
		this.residentsex = residentsex;
	}
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public String getBid() {
		return bid;
	}
	public void setBid(String bid) {
		this.bid = bid;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getRid() {
		return rid;
	}
	public void setRid(String rid) {
		this.rid = rid;
	}
	public Room getRoom() {
		return room;
	}
	public void setRoom(Room room) {
		this.room = room;
	}
	public Residents getResidents() {
		return residents;
	}
	public void setResidents(Residents residents) {
		this.residents = residents;
	}
	public List<HousingProperty> getHousingPropertys() {
		return housingPropertys;
	}
	public void setHousingPropertys(List<HousingProperty> housingPropertys) {
		this.housingPropertys = housingPropertys;
	}
	public List<Type> getTypes() {
		return types;
	}
	public void setTypes(List<Type> types) {
		this.types = types;
	}
	public String getRcode() {
		return rcode;
	}
	public void setRcode(String rcode) {
		this.rcode = rcode;
	}
	public ResidentsMember getMember() {
		return member;
	}
	public void setMember(ResidentsMember member) {
		this.member = member;
	}
	public String getTids() {
		return tids;
	}
	public void setTids(String tids) {
		this.tids = tids;
	}
	public String getRmins() {
		return rmins;
	}
	public void setRmins(String rmins) {
		this.rmins = rmins;
	}
	public String getRmaxs() {
		return rmaxs;
	}
	public void setRmaxs(String rmaxs) {
		this.rmaxs = rmaxs;
	}
	public String getPrices() {
		return prices;
	}
	public void setPrices(String prices) {
		this.prices = prices;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List<ResHydropower> getHydropowers() {
		return hydropowers;
	}
	public void setHydropowers(List<ResHydropower> hydropowers) {
		this.hydropowers = hydropowers;
	}
	
}
